package com.pounds.firstgame;

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private GameView mGameView;
    private TextView send;
    private EditText phone;
    private EditText text;
    private String defaultSmsApp;
    private String mDefaultSmsApp;
    private TextView setDefault;

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* mGameView=(GameView)findViewById(R.id.main_game_view);*/
        System.out.println(" sdk " + Build.VERSION.SDK_INT +"  " + Build.VERSION_CODES.KITKAT);
        if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            mDefaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this);
        }
        setDefault=(TextView)findViewById(R.id.set_default);
        phone=(EditText)findViewById(R.id.phone);
        text=(EditText)findViewById(R.id.contentText);
        send=(TextView)findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendSMS(phone.getText().toString(),text.getText().toString());
            }
        });
        setDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("setDefault");
                setDefault(view);
            }
        });
    }

    private void sendSMS(String phone,String content){
        try {
            ContentValues values = new ContentValues();
            values.put("address", phone);
            values.put("type", "1");
            values.put("read", "0");
            values.put("body", content);
            values.put("date", new Date().getTime());
            values.put("person", "test");
            getApplicationContext().getContentResolver()
                    .insert(Uri.parse("content://sms/inbox"), values);
            Log.e(getClass().getSimpleName(),"sendSMS success");
            clear();
        }catch (Exception e){
            Log.e(getClass().getSimpleName(),"sendSMS fail");
        }

    }

    private void clear() {
        phone.setText("");
        text.setText("");
        Toast.makeText(this,"clear",Toast.LENGTH_SHORT).show();
    }


    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void setDefault(View view){
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
        startActivity(intent);
    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void reset(View view){
        System.out.println("reset");
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,mDefaultSmsApp);
        startActivity(intent);
    }
}
